package com.shangguigu.linkedlist;

/**
 * @author: ZhouBert
 * @date: 2019/10/1
 * @description: 用第一种方式增加节点（结尾增加）
 */
public class SingleLinkedList4HeroAddLast implements SingleLinkedList4Hero {

	private HeroNode head = new HeroNode(0, "", "");

	/**
	 * 尾指针
	 */
	private HeroNode rear = head;

	@Override
	public void add(HeroNode node) {
		rear.setNode(node);
		rear = rear.getNode();
	}

	@Override
	public void updateByNo(HeroNode newNode) {
		HeroNode cur = head;
		while (cur != null) {
			///满足更新条件
			if (cur.getNo() == newNode.getNo()) {
				cur.setName(newNode.getName());
				cur.setNickname(newNode.getNickname());
				break;
			}
			cur = cur.getNode();
		}
	}

	@Override
	public void del(int no) {
		HeroNode cur = head.getNode();
		HeroNode pre = head;
		HeroNode temp=null;

		///if first node is the one.
//		if (cur.getNo()==no){
//			temp=cur;
//			pre.setNode(cur.getNode());
//			temp.setNode(null);
//		}
		while (cur != null) {
			///满足更新条件
			if (cur.getNo() == no) {
				temp=cur;
				pre.setNode(cur.getNode());
				temp.setNode(null);
				break;
			}
			cur = cur.getNode();
		}
	}
}
